package org.jground.rx.ui.zk.report;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.dbfs.IDBFile;
import org.jground.rx.IRXObject;
import org.jground.rx.IRXObjectSynopsis;
import org.jground.rx.IRXObjectUtils;
import org.jground.rx.RXDataState;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXSearchCriteria;
import org.jground.rx.RXSystem;
import org.jground.rx.docu.DocuUtils;
import org.jground.rx.report.RXReport;
import org.jground.rx.report.RXReportPage;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIRXListboxModel;
import org.jground.util.CollectionUtils;
import org.jground.util.L;
import org.jground.zk.zulX.TabboxX;

public class UIReportPanel extends TabboxX {

	private static Log logger = LogFactory.getLog(UIReportPanel.class);

	private RXReport rxReport;
	private RXSearchCriteria searchCriteria;
	private IDBFile csvFile;
	// addEventListener(Events.ON_SELECT, this);

	private List<IRXObjectSynopsis> filteredMods = new ArrayList<IRXObjectSynopsis>();
	private List<UIReportColumn> uiColumns = new ArrayList<UIReportColumn>();
	private List<UIReportPage> uiReportPages = new ArrayList<UIReportPage>();
	//
	private List<IRXObject> mods = new ArrayList<IRXObject>();

	private UIRXListboxModel model;

	public UIReportPanel(UIFrame uiFrame, RXReport rxReport) {
		this.rxReport = rxReport;
		model = new UIRXListboxModel(mods);
		L.debug(logger, "Start init of UIReportPanel", this.rxReport.getMrid());
		//
		for (RXReportPage rxReportPage : rxReport.getRXReportPages()) {
			UIReportPage uiReportPage = new UIReportPage(uiFrame, this,
					rxReportPage, model);
			addTab(rxReportPage.getLabel(), uiReportPage);
			uiReportPages.add(uiReportPage);
			CollectionUtils.addUnique(uiColumns, uiReportPage.getColumns());
		}
		try {
			this.csvFile = RXSystem
					.getInstance()
					.getFileService()
					.newFile("/tmp/" + System.currentTimeMillis(),
							"Report.html");
		} catch (Exception e) {
			logger.error(e, e);
		}
		reset();
	}

	private void initSearchCriteria() {
		searchCriteria = new RXSearchCriteria();
		searchCriteria.setIncludedDataStates(RXDataState.APPROVED,
				RXDataState.STORED);
		searchCriteria.addIncludedMoids(rxReport.getSourceMoid());
	}

	public void reset() {

		initSearchCriteria();
		filteredMods = RXObjectService.getInstance().search(searchCriteria);
		model.removeAll();
		model.addAll(filteredMods
				.toArray(IRXObjectUtils.EMPTY_RXDataObject_ARRAY));
		for (UIReportColumn uiColumn : uiColumns) {
			// List<String[]> valuesArray = new ArrayList<String[]>();
			uiColumn.updateFilter(filteredMods);
			// if(uiColumn.HasFilterControl()) {
			// uiColumn.GetFilterControl().Reset();
			// valuesArray.Clear();
			// String attref = uiColumn.GetMOColumn().GetAttRef();
			// foreach(IRXObject mod in filteredMods) {
			// valuesArray.AddUnique(mod.getValues(attref));
			// }
			// uiColumn.GetFilterControl().SetFilterValues(valuesArray.ToArray());
			// }
		}

		printCurrentPage();

	}

	private void printCurrentPage() {
		try {
			int index = getSelectedIndex();
			if (index != -1) {
				UIReportPage uiReportPage = uiReportPages.get(index);
				byte[] data = uiReportPage.writeToCSVFile();
				RXSystem.getInstance().getFileService().save(csvFile, data);
			}
		} catch (Exception e) {
			logger.error(e, e);
		}
	}

	public void update() {
		// Collect Filter
		initSearchCriteria();
		for (UIReportColumn uiColumn : uiColumns) {
			uiColumn.updateSearchCriteria(searchCriteria);
			// if(uiColumn.HasFilterControl()) {
			// String[] filterSelectedValues =
			// uiColumn.GetFilterControl().GetSelectedValues();
			// if(ArrayUtils.IsNotEmpty(filterSelectedValues)) {
			// String attref = uiColumn.GetMOColumn().GetAttRef();
			// searchCriteria.AddExactMatches(attref, filterSelectedValues);
			// }
			// }
			// Update Filters
		}
		filteredMods = RXObjectService.getInstance().search(searchCriteria);
		model.removeAll();
		model.addAll(filteredMods
				.toArray(IRXObjectUtils.EMPTY_RXDataObject_ARRAY));
		printCurrentPage();
	}

	public List<IRXObjectSynopsis> getFilteredData() {
		return filteredMods;
	}

	public IDBFile getCsvFile() {
		return csvFile;
	}

}